{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting keras==2.2.4\n",
      "  Downloading Keras-2.2.4-py2.py3-none-any.whl (312 kB)\n",
      "Requirement already satisfied: six>=1.9.0 in c:\\users\\13633\\.conda\\envs\\pytorch-py37\\lib\\site-packages (from keras==2.2.4) (1.16.0)\n",
      "Collecting pyyaml\n",
      "  Downloading PyYAML-6.0-cp37-cp37m-win_amd64.whl (153 kB)\n",
      "Collecting h5py\n",
      "  Downloading h5py-3.6.0-cp37-cp37m-win_amd64.whl (2.8 MB)\n",
      "Requirement already satisfied: scipy>=0.14 in c:\\users\\13633\\.conda\\envs\\pytorch-py37\\lib\\site-packages (from keras==2.2.4) (1.7.3)\n",
      "Collecting keras-applications>=1.0.6\n",
      "  Downloading Keras_Applications-1.0.8-py3-none-any.whl (50 kB)\n",
      "Collecting keras-preprocessing>=1.0.5\n",
      "  Downloading Keras_Preprocessing-1.1.2-py2.py3-none-any.whl (42 kB)\n",
      "Requirement already satisfied: numpy>=1.9.1 in c:\\users\\13633\\.conda\\envs\\pytorch-py37\\lib\\site-packages (from keras==2.2.4) (1.21.2)\n",
      "Collecting cached-property\n",
      "  Downloading cached_property-1.5.2-py2.py3-none-any.whl (7.6 kB)\n",
      "Installing collected packages: cached-property, h5py, pyyaml, keras-preprocessing, keras-applications, keras\n",
      "Successfully installed cached-property-1.5.2 h5py-3.6.0 keras-2.2.4 keras-applications-1.0.8 keras-preprocessing-1.1.2 pyyaml-6.0\n"
     ]
    }
   ],
   "source": [
    "!pip install keras==2.2.4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "LspltVTXeWkl"
   },
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'seaborn'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mModuleNotFoundError\u001B[0m                       Traceback (most recent call last)",
      "\u001B[1;32m~\\AppData\\Local\\Temp/ipykernel_11592/1888491294.py\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m      3\u001B[0m \u001B[1;32mimport\u001B[0m \u001B[0mnumpy\u001B[0m \u001B[1;32mas\u001B[0m \u001B[0mnp\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      4\u001B[0m \u001B[1;32mimport\u001B[0m \u001B[0mmatplotlib\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mpyplot\u001B[0m \u001B[1;32mas\u001B[0m \u001B[0mplt\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m----> 5\u001B[1;33m \u001B[1;32mimport\u001B[0m \u001B[0mseaborn\u001B[0m \u001B[1;32mas\u001B[0m \u001B[0msns\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m      6\u001B[0m \u001B[1;32mimport\u001B[0m \u001B[0mmatplotlib\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mdates\u001B[0m \u001B[1;32mas\u001B[0m \u001B[0mdates\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      7\u001B[0m \u001B[1;32mfrom\u001B[0m \u001B[0mdatetime\u001B[0m \u001B[1;32mimport\u001B[0m \u001B[0mdatetime\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;31mModuleNotFoundError\u001B[0m: No module named 'seaborn'"
     ]
    }
   ],
   "source": [
    "#importing important libraries\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import matplotlib.dates as dates\n",
    "from datetime import datetime\n",
    "from numpy import array\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "import tensorflow as tf\n",
    "from tensorflow import keras\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense\n",
    "from keras.layers import Flatten\n",
    "from keras.layers.convolutional import Conv1D\n",
    "from keras.layers.convolutional import MaxPooling1D\n",
    "\n",
    "from keras.datasets import mnist\n",
    "from keras.layers.core import Dense, Dropout, Activation, Flatten\n",
    "from keras.models import Sequential\n",
    "from keras.utils import np_utils\n",
    "\n",
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 204
    },
    "id": "rqEXNnSJe7ME",
    "outputId": "3e0472f6-17b5-490c-a7ab-f46484871dab"
   },
   "outputs": [],
   "source": [
    "df=pd.read_csv('AirPassengers.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 235
    },
    "id": "IG1rkGZd91Sc",
    "outputId": "11c89a93-9f40-4318-bffe-9ef463f3dbf9"
   },
   "outputs": [],
   "source": [
    "#Convert Month object into datetime\n",
    "df['Month'] = pd.to_datetime(df.Month)\n",
    "df = df.set_index(df.Month)\n",
    "df.drop('Month', axis = 1, inplace = True)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "MC1-jwR7e7JQ",
    "outputId": "9fa4f0d0-ed19-4a96-9b06-f357c8f041d2"
   },
   "outputs": [],
   "source": [
    "ts = df['#Passengers']\n",
    "ts.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "2usViZo3GsVm"
   },
   "source": [
    "# ANN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "JTcivrSG0_c5"
   },
   "outputs": [],
   "source": [
    "#Applying min max scaler for better fitting\n",
    "scaler= MinMaxScaler()\n",
    "ts=np.array(ts).reshape(-1,1)\n",
    "ts=scaler.fit_transform(ts)\n",
    "train_size=int(0.7*len(ts))\n",
    "test_size=len(ts)-train_size\n",
    "train=ts[0:train_size,:]\n",
    "print (train)\n",
    "\n",
    "\n",
    "\n",
    "test=ts[train_size:,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "PhWz8aXG1hb3"
   },
   "outputs": [],
   "source": [
    "#Function to create dataset\n",
    "def get_data(data, look_back):\n",
    "    dataX, dataY = [], []\n",
    "    for i in range(len(data)-look_back-1):\n",
    "        a = data[i:(i+look_back), 0]\n",
    "        dataX.append(a)\n",
    "        dataY.append(data[i+look_back, 0])\n",
    "    return np.array(dataX), np.array(dataY)\n",
    "look_back = 1\n",
    "#The below code provides me training and test data\n",
    "X_train, y_train = get_data(train, look_back)\n",
    "# X_test, y_test = get_data(x, look_back)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "wGhMfiwbBPcX"
   },
   "outputs": [],
   "source": [
    "#reshaping training data in order to build the model for LSTMs\n",
    "X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)\n",
    "X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "vVcM59qN1OAk",
    "outputId": "e1e57b66-30bb-4769-a8fc-10846ba711fe"
   },
   "outputs": [],
   "source": [
    "# define model\n",
    "model1 = keras.Sequential()\n",
    "model1.add(Dense(100,activation='relu',input_shape = (1,look_back)))\n",
    "model1.add(Dense(1))\n",
    "model1.compile(loss='mean_squared_error', optimizer='adam')\n",
    "model1.fit(X_train, y_train, epochs=80, batch_size=32, verbose=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "SNYBFBQdBoOV",
    "outputId": "b4381d44-cdf7-46ce-a4cd-fa5e145dbf17"
   },
   "outputs": [],
   "source": [
    "trainScore = model1.evaluate(X_train, y_train, verbose=0)\n",
    "print('Train Score: %.2f MSE (%.2f RMSE)' % (trainScore, math.sqrt(trainScore)))\n",
    "testScore = model1.evaluate(X_test, y_test, verbose=0)\n",
    "print('Test Score: %.2f MSE (%.2f RMSE)' % (testScore, math.sqrt(testScore)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 320
    },
    "id": "5bSJv5JHB1zV",
    "outputId": "e1900913-d2e4-43f7-9708-94162a0c63ef"
   },
   "outputs": [],
   "source": [
    "y_pred = model1.predict(X_test)\n",
    "y_pred1=y_pred.reshape(-1,1)\n",
    "#inverse transform because I wanted to plot actual values and not the normalized ones\n",
    "y_pred1 = scaler.inverse_transform(y_pred1)\n",
    "y_test1=y_test.reshape(-1,1)\n",
    "y_test1 = scaler.inverse_transform(y_test1)\n",
    "# plot baseline and predictions\n",
    "plt.figure(figsize=(14,5))\n",
    "plt.plot(y_test1, label = 'Real number of passengers')\n",
    "plt.plot(y_pred1, label = 'Predicted number of passengers')\n",
    "plt.ylabel('# passengers')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "EfsMrr1cGj1M"
   },
   "source": [
    "# Convolutional Neural Networks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "ZiaidDQQGxlG",
    "outputId": "7c761661-873b-43a8-cedc-6e1c508a3a9d"
   },
   "outputs": [],
   "source": [
    "# define model\n",
    "model2 = Sequential()\n",
    "model2.add(Conv1D(filters=10, kernel_size=1, activation='relu', input_shape=(1,look_back)))\n",
    "model2.add(Flatten())\n",
    "model2.add(Dense(50, activation='relu'))\n",
    "model2.add(Dense(1))\n",
    "model2.compile(optimizer='adam', loss='mse')\n",
    "# fit model\n",
    "model2.fit(X_train, y_train,batch_size=32 ,epochs=40, verbose=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "xQxdowzmFYZN",
    "outputId": "8802ee85-da1a-42ed-9912-6f6dd833c595"
   },
   "outputs": [],
   "source": [
    "trainScore = model2.evaluate(X_train, y_train, verbose=0)\n",
    "print('Train Score: %.2f MSE (%.2f RMSE)' % (trainScore, math.sqrt(trainScore)))\n",
    "testScore = model2.evaluate(X_test, y_test, verbose=0)\n",
    "print('Test Score: %.2f MSE (%.2f RMSE)' % (testScore, math.sqrt(testScore)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 357
    },
    "id": "0S_mXfOVGxe5",
    "outputId": "e340a843-93e9-472f-8ad8-fba187bc3e06"
   },
   "outputs": [],
   "source": [
    "y_pred = model2.predict(X_test)\n",
    "y_pred2=y_pred.reshape(-1,1)\n",
    "y_pred2 = scaler.inverse_transform(y_pred2)\n",
    "y_test2=y_test.reshape(-1,1)\n",
    "y_test2 = scaler.inverse_transform(y_test2)\n",
    "# plot baseline and predictions\n",
    "plt.figure(figsize=(14,5))\n",
    "plt.plot(y_test2, label = 'Real number of passengers')\n",
    "plt.plot(y_pred2, label = 'Predicted number of passengers')\n",
    "plt.ylabel('# passengers')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Qj55L61nKqQ6"
   },
   "source": [
    "# LSTMs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "AEADcgJWe7Ay",
    "outputId": "d91ce0f7-c4b3-40f7-e683-ab98b75d68db",
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# define model\n",
    "from keras.layers import LSTM\n",
    "model3 = Sequential()\n",
    "import time\n",
    " \n",
    "# 格式化成2016-03-20 11:45:39形式\n",
    "print (time.strftime(\"%Y-%m-%d %H:%M:%S\", time.localtime()))\n",
    "model3.add(LSTM(20, activation='relu', input_shape=(1,look_back)))\n",
    "model3.add(Dense(20, activation='relu'))\n",
    "model3.add(Dense(1))\n",
    "model3.compile(optimizer='Adam', loss='mse')\n",
    "\n",
    "\n",
    "\n",
    "# fit model\n",
    "model3.fit(X_train, y_train,batch_size=16, epochs=20, verbose=1)\n",
    "# Estimate model performance and plots\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "pUeyiD1mHQvd",
    "outputId": "cb9e1d25-a6b8-4759-db60-d0ba227e9659"
   },
   "outputs": [],
   "source": [
    "trainScore = model3.evaluate(X_train, y_train, verbose=0)\n",
    "print('Train Score: %.2f MSE (%.2f RMSE)' % (trainScore, math.sqrt(trainScore)))\n",
    "testScore = model3.evaluate(X_test, y_test, verbose=0)\n",
    "print('Test Score: %.2f MSE (%.2f RMSE)' % (testScore, math.sqrt(testScore)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 357
    },
    "id": "GpKIndwppc-g",
    "outputId": "981fe49a-bf9d-4ad2-e82c-c34c76612de5"
   },
   "outputs": [],
   "source": [
    "print (len(X_test))\n",
    "\n",
    "y_pred = model3.predict(X_test)\n",
    "print(len(y_pred))\n",
    "y_pred3= y_pred.reshape(-1,1)\n",
    "y_pred3 = scaler.inverse_transform(y_pred3)\n",
    "y_test3=y_test.reshape(-1,1)\n",
    "y_test3 = scaler.inverse_transform(y_test3)\n",
    "# plot baseline and predictions\n",
    "plt.figure(figsize=(14,5))\n",
    "plt.plot(y_test3, label = 'Real number of passengers')\n",
    "plt.plot(y_pred3, label = 'Predicted number of passengers')\n",
    "plt.ylabel('# passengers')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ugbNQbBBKAdt"
   },
   "source": [
    "### LSTM gave the best predictions."
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "Best Time series DL.ipynb",
   "provenance": [],
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}